using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;

using Chess;

namespace PDAChess
{
    public partial class ucLoadGames: UserControl
    {
        public delegate void Cancel_Click();
        public event Cancel_Click CancelClick;

        public delegate void Load_Click();
        public event Load_Click LoadClick;

        public ucLoadGames()
        {
            InitializeComponent();

            LoadSaveFiles();
        }

        public void LoadSaveFiles()
        {
            StringBuilder builder = new StringBuilder(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName.Replace(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].Name, ""));
            builder.Append(@"\save\");

            if (!Directory.Exists(builder.ToString()))
            {
                Directory.CreateDirectory(builder.ToString());
            }

            DirectoryInfo dicinfo = new DirectoryInfo(builder.ToString());
            FileInfo[] files = dicinfo.GetFiles("*.sav");

            lbSaves.Items.Clear();
            for (int i = 0; i < files.Length; i++)
            {
                lbSaves.Items.Add(new SaveFile(files[i]));
            }
            
            lbSaves.ValueMember="Path";
            lbSaves.DisplayMember="Name";
        }

        public string LayPathDaChon()
        {
            string kq = "";
            int idx = lbSaves.SelectedIndex;
            if (idx == -1)
                return kq;
            SaveFile dangChon = (SaveFile)lbSaves.Items[idx];
            kq += dangChon.Path;
            return kq;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (CancelClick != null)
            {
                CancelClick();
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (LoadClick != null)
            {
                LoadClick();
            }
        }


    }
}
